using System;
using System.Collections;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
using NHibernate;
using BEGDC.CC.BL;
using BEGDC.CC.Entity;

namespace BEGDC.CC.BL.Common.Log
{
	/// <summary>
	/// Summary description for LogService.
	/// </summary>
	/// 
	[Transactional]
	public class ActionLogService:IActionLog
	{
		private ISessionManager _sessionManager;


		/// <summary>
		/// Constructor.
		/// </summary>
		public ActionLogService(ISessionManager sessionManager)
		{
			this._sessionManager = sessionManager;
		}

		public ActionLogService()
		{
		}

		/// <summary>
		/// Add Action Log
		/// </summary>
		/// <param name="log">log to add</param>
		[Transaction(TransactionMode.Requires)]
		public void Add(ActionLog log, ISession session)
		{
			if( log == null )	return;
			
			try
			{
				if( log.Id == Convert.ToInt32( null )||log.Id ==0) 
					session.Save(log);
				else 
					session.Update(log);

			}
			catch(Exception exp)
			{
				CCS.Utility.LogAccess.WriteErrLog( exp.StackTrace, exp.Message );
				throw exp;
			}
			
		}

		/// <summary>
		/// Add Action Log
		/// </summary>
		/// <param name="log">log to add</param>
		[Transaction(TransactionMode.Requires)]
		public IList GetLog( int projectId, ISession session)
		{
			try
			{
				IList list = session.Find("from ActionLog where projectID="+ projectId.ToString());
				return list;
			}			
			catch(Exception exp)
			{
				CCS.Utility.LogAccess.WriteErrLog( exp.StackTrace, exp.Message );
				throw exp;
			}

			return null;
		}

		#region IActionLog Members
		
		/// <summary>
		/// Add Action Log
		/// </summary>
		/// <param name="log">log to add</param>
		[Transaction(TransactionMode.Requires)]
		public void Add(ActionLog log)
		{
			if( log == null )	return;
			
				using(ISession session = _sessionManager.OpenSession())
				{
					try
					{
						if( log.Id == Convert.ToInt32( null )||log.Id ==0) 
							session.Save(log);
						else 
							session.Update(log);
	
					}
					catch(Exception exp)
					{
						CCS.Utility.LogAccess.WriteErrLog( exp.StackTrace, exp.Message );
						throw exp;
					}
					finally
					{
						session.Close();
					}
				}
			
		}

		/// <summary>
		/// Delete Action Log List
		/// </summary>
		[Transaction(TransactionMode.Requires)]
		public void Clear()
		{
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					session.Delete("from ActionLog");
				}
				catch(Exception exp)
				{
					CCS.Utility.LogAccess.WriteErrLog( exp.StackTrace, exp.Message );
					throw exp;
				}
				finally
				{
					session.Close();
				}
			}
		}


		/// <summary>
		/// Get Action Log List
		/// </summary>
		[Transaction(TransactionMode.Requires)]
		public IList GetLog()
		{
			IList list;
			using(ISession session = _sessionManager.OpenSession())
			{
				try
				{
					list = session.Find("from ActionLog");
				}
				catch(Exception exp)
				{
					CCS.Utility.LogAccess.WriteErrLog( exp.StackTrace, exp.Message );
					throw exp;
				}
				finally
				{
					session.Close();
				}
			}
			return list;
			
		}

		#endregion
	}
}
